home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / glimpse-2.1 / agrep / checksg.c < prev    next >
C/C++ Source or Header  |  1995-05-16  |  4KB  |  157 lines

  1. /* Copyright (c) 1994 Sun Wu, Udi Manber, Burra Gopal.  All Rights Reserved. */
  2. #include "agrep.h"
  3. #include "checkfile.h"
  4.  
  5. extern int errno;
  6. extern CHAR Progname[MAXNAME]; 
  7. extern int SGREP, PAT_FILE, PAT_BUFFER, EXITONERROR, SIMPLEPATTERN,
  8.     CONSTANT, D, NOUPPER, JUMP, I, LINENUM, INVERSE, WORDBOUND, WHOLELINE,
  9.     SILENT, DNA, BESTMATCH;
  10.  
  11. /* Make it an interface routine that tells you whether mgrep can be used for the pattern or not: must sneak and access global variable D though... */
  12. checksg(Pattern, D, set)
  13. CHAR *Pattern; 
  14. int D;
  15. int set;    /* should I set flags SGREP and DNA? not if called from glimpse via library */
  16. {
  17.     char c;
  18.     int i, m;
  19.     int NOTSGREP = 0;
  20.  
  21.     if (set) SGREP = OFF;
  22.     m = strlen(Pattern);
  23. #if    DEBUG
  24.     fprintf(stderr, "checksg: len=%d, pat=%s, pat[len]=%d\n", m, Pattern, Pattern[m]);
  25. #endif
  26.     if(!(PAT_FILE || PAT_BUFFER) && (m <= D)) {
  27.         fprintf(stderr, "%s: size of pattern '%s' must be > #of errors %d\n", Progname, Pattern, D);
  28.         if (!EXITONERROR) {
  29.             errno = 2;
  30.             return -1;
  31.         }
  32.         else exit(2);
  33.     }
  34.     SIMPLEPATTERN = ON;
  35.     for (i=0; i < m; i++) 
  36.     {
  37.         switch(Pattern[i])
  38.         {
  39.         case ';' : 
  40.             SIMPLEPATTERN = OFF; 
  41.             goto outoffor;
  42.         case ',' : 
  43.             SIMPLEPATTERN = OFF; 
  44.             goto outoffor;
  45.         case '.' : 
  46.             SIMPLEPATTERN = OFF; 
  47.             goto outoffor;
  48.         case '*' : 
  49.             SIMPLEPATTERN = OFF; 
  50.             goto outoffor;
  51.         case '-' : 
  52.             SIMPLEPATTERN = OFF; 
  53.             goto outoffor;
  54.         case '[' : 
  55.             SIMPLEPATTERN = OFF; 
  56.             goto outoffor;
  57.         case ']' : 
  58.             SIMPLEPATTERN = OFF; 
  59.             goto outoffor;
  60.         case '(' : 
  61.             SIMPLEPATTERN = OFF; 
  62.             goto outoffor;
  63.         case ')' : 
  64.             SIMPLEPATTERN = OFF; 
  65.             goto outoffor;
  66.         case '<' : 
  67.             SIMPLEPATTERN = OFF; 
  68.             goto outoffor;
  69.         case '>' : 
  70.             SIMPLEPATTERN = OFF; 
  71.             goto outoffor;
  72.         case '^' : 
  73.             NOTSGREP = 1;
  74.             if(D > 0) SIMPLEPATTERN = OFF; 
  75.             goto outoffor;
  76.         case '$' : 
  77.             NOTSGREP = 1;
  78.             if(D > 0) SIMPLEPATTERN = OFF; 
  79.             goto outoffor;
  80.         case '|' : 
  81.             SIMPLEPATTERN = OFF; 
  82.             goto outoffor;
  83.         case '#' : 
  84.             SIMPLEPATTERN = OFF; 
  85.             goto outoffor;
  86.         case '{':
  87.             SIMPLEPATTERN = OFF;
  88.             goto outoffor;
  89.         case '}':
  90.             SIMPLEPATTERN = OFF;
  91.             goto outoffor;
  92.         case '~':
  93.             SIMPLEPATTERN = OFF;
  94.             goto outoffor;
  95.         case '\\' : 
  96.         {    /* Should I DO the left shift Pattern including Pattern[m] which is '\0', or just ignore the next character after '\\'????? */
  97.             int j;
  98.             if (set) {    /* preprocess and maskgen figure out what to do */
  99.                 i++;    /* in addition to for loop ++ */
  100.             }
  101.             else {    /* maskgen won't be called if we can help it, so shift it to make it verbatim */
  102.                 /*
  103.                 for (j=i; j<m; j++) Pattern[j] = Pattern[j+1];
  104.                 m --;
  105.                 */
  106.                 i++;
  107.             }
  108.             break;
  109.         }
  110.         default  : 
  111.             break;
  112.         }
  113.     }
  114.  
  115. outoffor:
  116.     if (CONSTANT) SIMPLEPATTERN = ON;
  117.     if (SIMPLEPATTERN == OFF) return 0;
  118.     if (BESTMATCH) return 0;    /* can have errors, not simple */
  119.     if (!set && (D>0)) return 0;    /* errors, not simple */
  120.     if (NOUPPER && (D>0)) return 0;    /* errors, not simple */     
  121.     if (JUMP == ON) return 0;    /* I, S, D costs, not simple */
  122.     if (I == 0) return 0;        /* I has 0 cost not 1, not simple */
  123.     if (LINENUM) return 0;        /* can't use mgrep, so not simple */
  124.     if (WORDBOUND && (D > 0)) return 0; /* errors, not simple */  
  125.     if (WHOLELINE && (D > 0)) return 0; /* errors, not simple */  
  126.     if (SILENT) return 1;        /* dont care output, so dont care pat */
  127.  
  128.     if (set) {
  129.         if (!NOTSGREP || CONSTANT) SGREP = ON;
  130.         if (m >= 16) DNA = ON;
  131.         for(i=0; i<m; i++) {
  132.             c = Pattern[i];
  133.             if(c == 'a' || c == 'c' || c == 't' || c == 'g' ) ;
  134.             else DNA = OFF;
  135.         }
  136.     }
  137.  
  138. #if    0
  139.     /* Ditch this: sgrep does it internally anyway */
  140.     if (SGREP) {    /* => set MUST be on */
  141.         for (i=0; i < m; i++) 
  142.         {
  143.         switch(Pattern[i])
  144.         {
  145.         case '\\' : 
  146.             for (j=i; j<m; j++) Pattern[j] = Pattern[j+1];
  147.             m --;
  148.             break;
  149.         default  : 
  150.             break;
  151.         }
  152.         }
  153.     }
  154. #endif    /*0*/
  155.     return 1;            /* remains simple */
  156. }
  157.